5 Lecture

CS201

Midterm & Final Term Short Notes

Conditional Statements

Conditional statements are programming constructs that allow a program to make decisions based on certain conditions. They use if-then logic to determine whether to execute certain sections of code. Conditional statements typically involve compa


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the syntax for an if statement in Python?

A. if (condition): B. if [condition]: C. if {condition}: D. if <condition>:

Answer: A

  1. What is the output of the following code snippet?

x = 5 if x > 3: print("x is greater than 3") else: print("x is less than or equal to 3")

A. x is greater than 3 B. x is less than or equal to 3 C. SyntaxError D. NameError

Answer: A

  1. Which conditional statement can handle multiple conditions in Python?

A. if statement B. if-else statement C. if-elif statement D. switch statement

Answer: C

  1. What is the syntax for an if-else statement in JavaScript?

A. if (condition) {code block 1;} else {code block 2;} B. if [condition] {code block 1;} else {code block 2;} C. if {condition} {code block 1;} else {code block 2;} D. if <condition> {code block 1;} else {code block 2;}

Answer: A

  1. What is the output of the following code snippet?

x = 10 if x > 5: print("x is greater than 5") elif x > 8: print("x is greater than 8") else: print("x is less than or equal to 5")

A. x is greater than 5 B. x is greater than 8 C. x is less than or equal to 5 D. SyntaxError

Answer: A

  1. Which conditional statement is used to execute different code blocks for different values of a variable?

A. if statement B. if-else statement C. if-elif statement D. switch statement

Answer: D

  1. What is the output of the following code snippet?

x = 10 if x > 5 and x < 15: print("x is between 5 and 15")

A. x is between 5 and 10 B. x is between 10 and 15 C. x is greater than 5 and less than 15 D. x is not between 5 and 15

Answer: C

  1. Which of the following is an example of a Boolean expression?

A. 5 + 3 B. "Hello, world!" C. True D. None

Answer: C

  1. What is the output of the following code snippet?

x = "apple" if "p" in x: print("The letter p is in x")

A. The letter p is in x B. The letter p is not in x C. SyntaxError D. NameError

Answer: A

  1. What is the output of the following code snippet?

x = 5 if x == 5: x = x + 1 if x > 5: x = x + 2 print(x)

A. 5 B. 6 C. 7 D. 8

Answer: C



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a conditional statement? Answer: A conditional statement is a programming construct that allows a program to make decisions based on certain conditions. It uses if-then logic to determine whether to execute certain sections of code.

  2. What is the difference between if and if-else statements? Answer: An if statement executes a section of code if a condition is true. An if-else statement executes one section of code if the condition is true, and another section of code if the condition is false.

  3. What is the syntax for an if statement in Java? Answer: The syntax for an if statement in Java is: if (condition) { // code to execute if condition is true }

  4. What is a Boolean expression? Answer: A Boolean expression is an expression that evaluates to either true or false.

  5. What is the purpose of the else keyword in an if-else statement? Answer: The else keyword in an if-else statement is used to execute a section of code if the condition in the if statement is false.

  6. What is the syntax for an if-elif statement in Python? Answer: The syntax for an if-elif statement in Python is: if condition1:

    code to execute if condition1 is true

elif condition2: # code to execute if condition2 is true else: # code to execute if neither condition1 nor condition2 is true

  1. What is the difference between the == and = operators in Python? Answer: The == operator is used to compare two values for equality, while the = operator is used to assign a value to a variable.

  2. What is short-circuit evaluation in conditional statements? Answer: Short-circuit evaluation is a process in which a conditional statement stops evaluating expressions as soon as it can determine the outcome based on earlier evaluations.

  3. What is the syntax for a switch statement in C++? Answer: The syntax for a switch statement in C++ is: switch (expression) { case value1: // code to execute if expression == value1 break; case value2: // code to execute if expression == value2 break; default: // code to execute if expression does not match any of the cases }

  4. What is the purpose of the break keyword in a switch statement? Answer: The break keyword in a switch statement is used to exit the switch statement after executing the code in the matching case. Without a break statement, the code in the next case will also be executed.

Conditional statements are a fundamental part of programming, allowing developers to make decisions and control the flow of a program based on certain conditions. In programming, conditions are evaluated using Boolean expressions, which can be either true or false. The most basic type of conditional statement is the if statement, which executes a section of code if a condition is true. The syntax for an if statement is as follows: if (condition) { // code to execute if condition is true } If the condition is false, the code inside the if statement is skipped. An if-else statement is similar to an if statement, but includes an else block that is executed if the condition is false. The syntax for an if-else statement is: if (condition) { // code to execute if condition is true } else { // code to execute if condition is false } In more complex programs, multiple conditions may need to be evaluated. This is where the if-elif-else statement comes in. The syntax for an if-elif-else statement is: if condition1: # code to execute if condition1 is true elif condition2: # code to execute if condition2 is true else: # code to execute if neither condition1 nor condition2 is true The switch statement is another type of conditional statement, used in languages like C++ and Java. It allows for more efficient and readable code when there are multiple conditions to evaluate. The syntax for a switch statement in C++ is: switch (expression) { case value1: // code to execute if expression == value1 break; case value2: // code to execute if expression == value2 break; default: // code to execute if expression does not match any of the cases } In addition to if and switch statements, many programming languages also include ternary operators, which provide a shorthand way of writing if-else statements. The syntax for a ternary operator is: condition ? value_if_true : value_if_false Overall, conditional statements are a powerful tool for developers to control the flow of their programs and make decisions based on user input or other conditions. Understanding how to write and use conditional statements is essential for any aspiring programmer.